package office.service.sale.impl;

import java.util.ArrayList;
import java.util.List;

import office.action.sale.form.SaleChanceConditionForm;
import office.action.sale.form.SaleChanceForm;
import office.action.sale.wrapper.SaleChanceWrapper;
import office.bo.sale.ISaleChance;
import office.bo.sale.impl.SaleChance;
import office.domainset.BizDomainProviderSetImpl;
import office.service.sale.ISaleChanceService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;

public class SaleChanceServiceImpl extends BaseBizServiceImpl implements ISaleChanceService{
	
	public void editSaleChance(SaleChanceForm saleChanceForm)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			SaleChance bo = (SaleChance)domainProviderSetImpl.getSaleChanceDP().newEmptyObj();
			bo =(SaleChance) saleChanceForm.copyPropertiesToBO(bo);
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑销售机会信息失败!", e);
		}
	}
	
	public Page getPageInfo()throws BusinessException{
		Page page = new Page();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			ISaleChance saleChance = (ISaleChance)domainProviderSetImpl.getSaleChanceDP().newEmptyObj();
			page = saleChance.getPageInfo(domainProviderSet);
		}catch(Exception e){
			log.error("", e);
			this.handleBusinessServiceException("获得分页信息失败", e);
		}	
		return page;
	}
	/**
	 * 
	 * <br/>方法描述:条件查询显示分页信息
	 * @param condition
	 * @return
	 */
	public Page getPageInfoByCondition(SaleChanceConditionForm condition)throws BusinessException{
		Page page = new Page();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			ISaleChance prodPriceData = (ISaleChance)domainProviderSetImpl.getSaleChanceDP().newEmptyObj();
			page = prodPriceData.getPageInfoByCondition(domainProviderSet, condition);
		} catch (Exception e) {
			log.error("", e);
			this.handleBusinessServiceException("获得分页信息失败", e);
		}
		return page;
	}
	public List<SaleChanceWrapper> findAllPriceData(Page page)throws BusinessException{
		List<SaleChanceWrapper> saleChanceWrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getSaleChanceDP();
			List list = domainProviderSetImpl.getDao().findAllByDomian(provider,page);
			SaleChanceWrapper wrapper;
			for(int i=0;i<list.size();i++){
				SaleChance bo = (SaleChance)list.get(i);
				wrapper = new SaleChanceWrapper();
				wrapper.copyToWrapper(bo);
				saleChanceWrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取销售机会列表失败！", e);
		}
		return saleChanceWrapperList;
	}
	/**
	 * 
	 * <br/>方法描述: 查询
	 * @return
	 */
	public List<SaleChanceWrapper> findAllSaleChanceByCondition(SaleChanceConditionForm condition,Page page) throws BusinessException {
		List<SaleChanceWrapper> saleChanceWrapperList = null;
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			ISaleChance saleChance = (ISaleChance)domainProviderSetImpl.getSaleChanceDP().newEmptyObj();
			saleChanceWrapperList = saleChance.findAllSaleChanceByCondition(domainProviderSet, condition, page);
			
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取销售机会列表失败！", e);
		}
		return saleChanceWrapperList;
	}
	
	public void delSaleChanceManageById(String id)throws BusinessException{
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getSaleChanceDP();
			domainProviderSetImpl.getDao().deleteById(provider, id);
		}catch(Exception e){
			log.error("", e);
			throw this.handleBusinessServiceException("删除销售机会失败！", e);
		}
	}
	
	public SaleChanceForm getById(String id)throws BusinessException{
		SaleChanceForm form = new SaleChanceForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			ISaleChance bo = (ISaleChance)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getSaleChanceDP(), id);
			form.copyPropertiesFromBO(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取销售机会失败!", e);
		}
		return form;
	}
}
